package cn.cytong.nk0713.services;

import cn.cytong.nk0713.utils.nutz.CustomisedNutDao;
import cn.cytong.rpc.server.NutzRpcService;
import flexjson.JSONSerializer;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.entity.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 旅游卡用户信息
 */
public class CustomerService extends NutzRpcService {
    private Logger logger = LoggerFactory.getLogger(CustomerService.class);
    private CustomisedNutDao nutDao;

    /**
     * 获取用户信息
     * @param params    id      用户Id
     *                  idNo    用户证件号
     * @return          customer    用户信息
     *                  card        用户绑定卡片信息
     *                  success     true or false
     */
    public String getCustomerInfo(Map params) {
        Map result = new HashMap();
        Object id = params.get("id");
        String idNo = (String) params.get("idNo");
        try {
            Record customer = null;
            if (id != null)
                customer = nutDao.fetchTableRecord("customer", "id = ?", id);
            else if (StringUtils.isNotBlank(idNo))
                customer = nutDao.fetchTableRecord("customer", "idNo = ?", idNo);
            checkNotNull(customer, "用户不存在");
            Record card = nutDao.fetchTableRecord("card", "customer_id = ? and state = 1", customer.get("id"));
            result.put("card", card);
            result.put("customer", customer);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("error in method customerService.getCustomerInfo", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return new JSONSerializer().serialize(result);
    }

}
